// pages/me/index.js
var util = require('../../utils/util.js');

Component({

  /**
   * 页面的初始数据
   */
  data: {
    userDetails: '',
    recomUid: '',
    menuList: [{
      icon: '/assets/image/me/1.png',
        title: '订单管理',
        link: '/pages/orderList/index'
      },
      {
        icon: '/assets/image/me/2.png',
        title: '账户明细',
        link: '/pages/accountDetails/index'
      },
      {
        icon: '/assets/image/me/3.png',
        title: '关注收藏',
        link: '/pages/focusOn1/index'
      },
      {
        icon: '/assets/image/me/4.png',
        title: '出行人管理',
        link: '/pages/management/index'
      },
      {
        icon: '/assets/image/me/5.png',
        title: '个人设置',
        link: '/pages/userInfo/index'
      },
      {
        icon: '/assets/image/me/6.png',
        title: '邀请好友',
        link: '/pages/friend/index'
      },
      {
        icon: '/assets/image/me/7.png',
        title: '我要反馈',
        link: '/pages/feedback/index'
      },
      {
        icon: '/assets/image/me/8.png',
        title: '我要入驻',
        link: '/pages/entering/index'
      }
    ]
  },

  methods: {
    toTransfer() {
      // 
      if (this.data.userInfo === '') {
        wx.showToast({
          title: '未登录',
          icon: 'none',
          duration: 2000
        })
        return false;
      }
      wx.navigateTo({
        url: '/pages/transfer/index',
      })
    },
    // 去余额提现界面
    toWithdraw() {
      if (this.data.userInfo === '') {
        wx.showToast({
          title: '未登录',
          icon: 'none',
          duration: 2000
        })
        return false;
      }
      wx.navigateTo({
        url: '/pages/withdraw/index?balance=' + this.data.userDetails.money,
      })
    },
    goMenuDetail(event) {
      if (this.data.userInfo === '') {
        wx.showToast({
          title: '未登录',
          icon: 'none',
          duration: 2000
        })
        return false;
      }
      if (event.currentTarget.dataset.link === 3) {
        wx.reLaunch({
          url: event.currentTarget.dataset.link,
        })
      } else {
        wx.navigateTo({
          url: event.currentTarget.dataset.link,
        })
      }
    },
    outLogin() {
      wx.removeStorage({
        key: 'userInfo',
        success(res) {
          wx.reLaunch({
            url: '/pages/index/index?id=4'
          })
        }
      })
    },
    // 登陆事件
    getUserInfo(e) {
      let that = this;
      wx.login({
        success(res) {
          if (res.code) {
            // 发起网络请求
            let postData = {
              code: res.code,
              encryptedData: e.detail.encryptedData,
              iv: e.detail.iv
            }
            if (that.data.recomUid !== '') {
              postData.recom_uid = that.data.recomUid;
            }
            util.http({
              url: 'api/miniwechat/user/dologin',
              data: postData,
              method: 'POST'
            }).then((data) => {
              if (data.userinfo.mobile) {
                wx.setStorage({
                  key: 'userInfo',
                  data: data.userinfo
                })
                wx.showToast({
                  title: '登录成功',
                  icon: 'none',
                  duration: 1000
                })
                setTimeout(function () {
                  wx.reLaunch({
                    url: '/pages/index/index'
                  })
                }, 1000)
              } else {
                wx.setStorage({
                  key: 'backUserInfo',
                  data: data.userinfo
                })
                wx.navigateTo({
                  url: '/pages/bindPhone/index?userId=' + data.userinfo.user_id
                })
              }
              
            }).catch((err) => {
              wx.showToast({
                title: String(err),
                icon: 'none',
                duration: 2000
              })
            })
          } else {
            console.log('登录失败！' + res.errMsg)
          }
        }
      })

    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  ready: function(options) {
    let that =this;
    wx.getStorage({
      key: 'recomUid',
      success(res) {
        that.data.recomUid = res.data
        console.log(res.data)
      }
    })
    this.setData({
      userInfo: util.isLogin()
    })
    if (this.data.userInfo === '') {
      return false;
    }
    util.http({
      url: 'api/miniwechat/center/personal',
      data: {
        user_id: this.data.userInfo.user_id
      }
    }).then(res => {
      console.log(res);
      res.score = parseInt(res.score);
      res.frozen_score = parseInt(res.frozen_score);
      this.setData({
        userDetails: res
      })
    }).catch(ex => {
      wx.showToast({
        title: String(ex),
        icon: 'none',
        duration: 2000
      })
    })
  },


})